package com.atguigu.web;

import com.atguigu.pojo.Book;
import com.atguigu.pojo.Page;
import com.atguigu.pojo.User;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BookServlet", value = "/BookServlet")
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
    }

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //             1.获取请求的参数pageNo和pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.page_size);

        System.out.println(request.getParameter("pageNo"));
        System.out.println(request.getParameter("pageSize"));

//             2.调用BookService.page(pageNo,pageSize):Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);
        page.setUrl("manager/bookServlet?action=page");
//             3.保存page到request域中
        request.setAttribute("page",page);

//             4.请求转发到pages/manager/book_manager.jsp页面
request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);

    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),0);
pageNo+=1;
        //     1.获取请求参数==封装book对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());

//     2.调用bookService.addBook保存book
        bookService.addBook(book);

//     3.跳转到图书列表或页面
//request.getRequestDispatcher("/manager/bookServlet?action=list").forward(request,response);

        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo=" + pageNo);

    }
    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       /* String id = request.getParameter("id");
        int i = Integer.parseInt(id);*/
        int id = WebUtils.parseInt(request.getParameter("id"),0);
        bookService.deleteById(id);
        response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+ request.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());

bookService.updateById(book);

response.sendRedirect(request.getContextPath()+"/manager/bookServlet?action=page&pageNo="+request.getParameter("pageNo"));
    }

    protected void getbook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//      1.获取图书的id
        int id = WebUtils.parseInt(request.getParameter("id"),0);

//        2.调用service.queryById查询图书对象
        Book book = bookService.queryBookById(id);

//        3.将对象数据保存到request域中
        request.setAttribute("book",book);

//        4.请求转发到/pages/manager/book_edit.jsp页面
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //             1.通过BookService查询所有图书

        List<Book> books =  bookService.queryBooks();

//             2.把所有图书保存到Request域中
        request.setAttribute("books",books);
//             3.请求转发到/pages/manager/book_manager.jsp页面
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);


    }
}
